package com.yyx.jupiter.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JsonUtils {
    private static final ThreadLocal<ObjectMapper> objectMapper = ThreadLocal.withInitial(() -> {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
        return mapper;
    });

    public static String toJsonString(Object obj) {
        try {
            return objectMapper.get().writeValueAsString(obj);
        } catch (Exception e) {
            log.error("err toJsonString has error = ", e);
            throw new RuntimeException(e);
        }
    }
}
